//
// Created by luolijun on 2021/11/7.
//

#include "ProjectEditorWidget.h"

namespace falcon {

void LoadPackage(QTreeWidgetItem* parent, Package* package) {
    for (auto item : package->diagrams) {
        parent->addChild(new QTreeWidgetItem(QStringList() << QString(item->name.c_str())));
    }

    for (auto item : package->objects) {
        if (item->type == "package") {
            QTreeWidgetItem* treeItem = new QTreeWidgetItem(QStringList() << QString(item->name.c_str()));
            parent->addChild(treeItem);
            LoadPackage(treeItem, (Package*)item);
        }
    }
}

ProjectEditorWidget::ProjectEditorWidget(QWidget* parent)
    : QTreeWidget(parent) {
    _project = nullptr;
}

void ProjectEditorWidget::attach(falcon::Project* project) {
    Q_ASSERT(project != nullptr);

    this->clear();
    _project = project;

    QTreeWidgetItem* root = new QTreeWidgetItem(QStringList() << QString(project->name.c_str()));
    addTopLevelItem(root);
    LoadPackage(root, _project);
}

} // namespace falcon